﻿using System;
using System.Collections.ObjectModel;
using StockholmsStad.Windows.UI.Modules.Ceremony.Interfaces;
using StockholmsStad.Windows.UI.Modules.Ceremony.Models;
using Microsoft.Practices.Composite.Events;

namespace StockholmsStad.Windows.UI.Modules.Ceremony.Views
{
    /// <summary>
    /// Interaction logic for CeremonyScheduleView.xaml
    /// </summary>
    public partial class CeremonyDetailsView : ICeremonyDetailsView
    {
        private readonly IEventAggregator eventAggregator;

        public CeremonyDetailsView(IEventAggregator aggregator)
        {
            InitializeComponent();

            eventAggregator = aggregator;
            //Vi måste manuellt skicka vidare aggregatorn
            ctrlPreviousCeremonies.Aggregator = aggregator;
        }

        #region ICeremonyDetailsView Members

        public ObservableCollection<CeremonyDetailsPresentationModel> Model
        {
            get { return DataContext as ObservableCollection<CeremonyDetailsPresentationModel>; }
            set 
            {
                if (value.Count > 0)
                {
                    ListType type = value[0].BookingDate < DateTime.Today ? ListType.Previous : ListType.Next;

                    ctrlPreviousCeremonies.Model = type == ListType.Previous ? value : null;
                    ctrlNextCeremonies.Model = type == ListType.Next ? value : null;

                    ctrlPreviousCeremonies.Visibility = ctrlPreviousCeremonies.DataContext == null ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible;
                    ctrlNextCeremonies.Visibility = ctrlNextCeremonies.DataContext == null ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible;
                }
                else
                {
                    ctrlPreviousCeremonies.Model = null;
                    ctrlNextCeremonies.Model = null;

                    ctrlPreviousCeremonies.Visibility = System.Windows.Visibility.Collapsed;
                    ctrlNextCeremonies.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
        }

        private enum ListType
        {
            Previous,
            Next
        }

        #endregion
    }
}
